package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import Item.StudentLogin;
import db.DBUtil;

public class UserDaoImpl implements UserDao {
	
	
	@Override
	public boolean isLogin(StudentLogin stulg) {
		boolean ret = false;
		//连接数据的三个主要对象：Connection,PreparedStament,ResultSet
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			//1.加载驱动
			//2.创建连接
			conn = DBUtil.getConnection();
			//3.写出SQL语句并创建执行器
			//3.1.写出SQL
			String sql = "select password from student where studentnumber=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, stulg.getStudentnumber());
			//4.执行SQL语句
			rs = pstmt.executeQuery();
			//5.处理结果
			if(rs.next()){
				String password = rs.getString("password");
				
				if (password!=null && password.equals(stulg.getPassword())){
					//1）先判断是否为空;2)&&是否相同
					ret = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//6.释放资源
			DBUtil.closeAll(null, conn, pstmt);
		}
		return ret;
	}

//	@SuppressWarnings("null")
//	@Override
//	public boolean isRegist(student stu) {
//		// TODO Auto-generated method stub
//		boolean ret = false;
//		//连接数据的三个主要对象：Connection,PreparedStament,ResultSet
//		Connection conn = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		
//		try {
//			//1.加载驱动
//			//2.创建连接
//			conn = DBUtil.getConnection();
//			//3.写出SQL语句并创建执行器
//			//3.1.写出SQL
//			String sql = "insert into student (id,studentnumber,password,realname,phone,email) values(?,?,?,?,?,?)";//?:鍗犱綅绗�
//			pstmt = conn.prepareStatement(sql);
//			pstmt.setInt(1, stu.getId());
//			pstmt.setString(2, stu.getStudentnumber());
//			pstmt.setString(3, stu.getPassword());
//			pstmt.setString(4, stu.getRealname());
//			pstmt.setString(5, stu.getPhone());
//			pstmt.setString(6, stu.getEmail());
//			//4.执行SQL语句
//			int num = pstmt.executeUpdate();
//			//5.处理结果
//			if (num!=0){
//				ret = true;
//			}
//			if(rs.next()){
//				stu=new student();
//				stu.setId(rs.getInt(1));
//				stu.setStudentnumber(rs.getString(2));
//				stu.setPassword(rs.getString(3));
//				stu.setRealname(rs.getString(4));
//				stu.setPhone(rs.getString(5));
//				stu.setEmail(rs.getString(6));
//		} }catch (SQLException e) {
//			e.printStackTrace();
//		} finally {
//			//6.释放资源
//			DBUtil.closeAll(null, conn, pstmt);
//		}
//		
//		return ret;
//	}
	}


